package com.sfh.appversion.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sfh.appversion.base.exception.ServiceException;
import com.sfh.appversion.base.page.PageResponse;
import com.sfh.appversion.dao.entity.AppVersionDO;
import com.sfh.appversion.dao.entity.PluginInfoDO;
import com.sfh.appversion.dao.entity.PluginVersionDO;
import com.sfh.appversion.dao.mapper.PluginInfoMapper;
import com.sfh.appversion.dao.mapper.PluginVersionMapper;
import com.sfh.appversion.dto.req.PluginEditReqDTO;
import com.sfh.appversion.dto.req.PluginNewReqDTO;
import com.sfh.appversion.dto.req.PluginPageReqDTO;
import com.sfh.appversion.dto.resp.PluginRespDTO;
import com.sfh.appversion.server.PluginServer;
import com.sfh.appversion.server.error.PluginErrorCodeEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

@Slf4j
@Service
@RequiredArgsConstructor
public class PluginServerImpl implements PluginServer {


    private final PluginInfoMapper pluginInfoMapper;
    private final PluginVersionMapper pluginVersionMapper;

    @Override
    public boolean add(PluginNewReqDTO vo) {

        LambdaQueryWrapper<PluginInfoDO> queryWrapper = Wrappers.lambdaQuery(PluginInfoDO.class)
                .eq(PluginInfoDO::getAppId, vo.getAppId())
                .eq(PluginInfoDO::getPluginPackageName, vo.getPluginPackageName());

        if (pluginInfoMapper.exists(queryWrapper)) {
            //插件存在，请检查！
            throw new ServiceException(PluginErrorCodeEnum.EXIST_ERROR);
        }
        PluginInfoDO pluginInfoDO = PluginInfoDO.builder()
                .appId(vo.getAppId())
                .pluginPackageName(vo.getPluginPackageName())
                .pluginName(vo.getPluginName())
                .pluginStatue(vo.getPluginStatue())
                .pluginContent(vo.getPluginContent())
                .build();
        return pluginInfoMapper.insert(pluginInfoDO) > 0;
    }

    @Override
    public boolean edit(PluginEditReqDTO vo) {

        PluginInfoDO pluginInfoDO = pluginInfoMapper.selectById(vo.getPluginId());
        if (pluginInfoDO == null) {
            //插件不存在，请检查！
            throw new ServiceException(PluginErrorCodeEnum.UNKNOWN_ERROR);
        }
        pluginInfoDO.setPluginName(vo.getPluginName());
        pluginInfoDO.setPluginContent(vo.getPluginContent());
        pluginInfoDO.setPluginStatue(vo.getPluginStatue());

        return pluginInfoMapper.updateById(pluginInfoDO) > 0;
    }

    @Override
    public boolean editStatue(String pulginId, String pluginStatue) {
        PluginInfoDO pluginInfoDO = pluginInfoMapper.selectById(pulginId);
        if (pluginInfoDO == null) {
            //插件不存在，请检查！
            throw new ServiceException(PluginErrorCodeEnum.UNKNOWN_ERROR);
        }
        pluginInfoDO.setPluginStatue(pluginStatue);
        return pluginInfoMapper.updateById(pluginInfoDO) > 0;
    }

    @Override
    public boolean del(String pulginId) {
        return pluginInfoMapper.deleteById(pulginId) > 0;
    }

    @Override
    public PluginInfoDO queryInfo(String pulginId) {
        return pluginInfoMapper.selectById(pulginId);
    }

    @Override
    public List<PluginRespDTO> queryList(String appId) {

        LambdaQueryWrapper<PluginInfoDO> queryWrapper = Wrappers.lambdaQuery(PluginInfoDO.class)
                .eq(PluginInfoDO::getAppId, appId);
        List<PluginInfoDO> pluginInfoDOS = pluginInfoMapper.selectList(queryWrapper);

        List<PluginRespDTO> list = new ArrayList<>();
        if (pluginInfoDOS != null) {
            pluginInfoDOS.forEach(new Consumer<PluginInfoDO>() {
                @Override
                public void accept(PluginInfoDO infoDO) {

                    PluginRespDTO dto = PluginRespDTO.transform(infoDO);
                    //查询插件最新版本
                    LambdaQueryWrapper<PluginVersionDO> lastWrapper = Wrappers.lambdaQuery(PluginVersionDO.class)
                            .eq(PluginVersionDO::getPulginId, infoDO.getPulginId())
                            .orderByAsc(PluginVersionDO::getVersionCode);//按照版本code 降序;
                    PluginVersionDO versionDO =  pluginVersionMapper.selectOne(lastWrapper);
                    if (versionDO != null){
                        dto.setPluginVersion(versionDO.getVersionCode()+"");
                    }
                    list.add(dto);
                }
            });
        }

        return list;
    }

}
